package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.SysTag;
import com.ruoyi.system.service.ISysTagService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.error;

/**
 * 标签类别
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/sysTag")
public class SysTagController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysTagService sysTagService;

    @ApiOperation("更新用户安全设置")
    @PostMapping("/saveorupdate")
    public AjaxResult update(@RequestBody SysTag sysTag) {
        if (sysTag == null) {
            return error("标签类别信息为空");
        }
        Integer count = 0;
        if (sysTag.getId() == null) {
            // 新增
            count = sysTagService.insert(sysTag);
        } else {
            // 更新
            count = sysTagService.updateById(sysTag);
        }
        if (count > 0) {
            return AjaxResult.success("保存成功");
        } else {
            return AjaxResult.error("保存失败");
        }
    }

    @ApiOperation("查询标签类别树结构")
    @GetMapping("/list")
    public AjaxResult getTreeList() {
        List<SysTag> sysTags = sysTagService.getTreeList();
        return AjaxResult.success(sysTags);

    }

    @ApiOperation("查询标签类别")
    @GetMapping("/getList")
    public AjaxResult getList(SysTag sysTagParam) {
        List<SysTag> sysTags = sysTagService.getList(sysTagParam);
        List<SysTag> newList = new ArrayList<>();
        for (SysTag sysTag : sysTags) {
            sysTag.setChildren(newList);
        }
        return AjaxResult.success(sysTags);
    }


    @ApiOperation("查询用户安全设置")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("id") Integer id) {
        Integer num = sysTagService.deleteById(id);
        if(num>0){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败");
        }
    }

}
